<?php
##############################################################################################
#	Compress URL (http://code.google.com/p/compressurl)
#	Author : Abhishek Verma (vermaabhishekp@gmail.com)
#	
#	This file is part of Compress URL.
#	
#	Compress URL is free software: you can redistribute it and/or modify
#	it under the terms of the GNU General Public License as published by
#	the Free Software Foundation, either version 3 of the License, or
#	(at your option) any later version.
#	
#	Compress URL is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#	
#	You should have received a copy of the GNU General Public License
#	along with Compress URL.  If not, see <http://www.gnu.org/licenses/>.
#	
# api.php:
#
#		Send get variables "url", "tiny" and "check" to this script
##############################################################################################

require_once("compress_url_libs/functions.inc.php");

//$DEBUG = true;

if ( $DEBUG ) print_r($_GET);
// First things first
prevent_mysql_injection();

$from = $_GET['url'];
$to = $_GET['tiny'];

// No from and to :(
if ( $from == "" && $to == "" )  {
	echo "Pass ?url=[URL you want to shorten]&tiny=[shortened url you want]&check=[if you want to check availability and not compress]";
	exit;
}

// Add "http://" if not already present
if( strpos ( $from, "http://" ) === false )
	$from = "http://".$from;

// Validate the to url
$validate = preg_match("/[a-z0-9_-]+/", $to, $array);

// 0th element should be same as "to" if regex was matched fully
if($array[0] != $to ){
   //error message
   echo "\"Tiny\" URL can contain a-z, 0-9 or _ chars only";
   exit;
}

// Only to is present
if ($from != "" && $to=="") {
	// Check availability
	$r = query("select * from `". $MYSQL_TABLE ."` where `from`='". $from ."' limit 1");
	if(mysql_num_rows($r) > 0) {
		$a = mysql_fetch_array($r);
		echo get_path().$a['to'];
	}
	exit;
}

// User just wants to check and not compress
if(isset($_GET['check'])) {
	$r = query ( "select * from `" . $MYSQL_TABLE . "` where `to`='". $to ."'" );

	if($a = mysql_fetch_array($r)) {
		echo "Not available";
	}
	else {
		echo "Available";
	}
	exit;
}

// Only from is available. Show him the tinied version
if ( (!isset($_GET['url']) || $from == "")  && $to!="") {
	// Check availability
	$r = query("select * from `". $MYSQL_TABLE ."` where `to`='". $to ."' limit 1");
	if(mysql_num_rows($r) > 0) {
		$a = mysql_fetch_array($r);
		echo $a['from'];
	}
	exit;
}

// From passed, but not to
if ( $to == "" )  {
	echo "Pass ?url=URL you want to shorten&tiny=shortened url you want";
	exit;
}

// Compress
// Check if "to" is already taken
$r = query ( "select * from `" . $MYSQL_TABLE . "` where `to`='". $to ."'" );

if($a = mysql_fetch_array($r)) {

	echo "Not available";
}
else {
	// "to" is available
	query( "INSERT INTO `". $MYSQL_TABLE ."` ( `id` , `to` , `from` ) VALUES ('', '". $to ."', '". $from ."');");

	echo get_path().$to;
}
?>